/*
* $Id: AddInspectForm.java 270 2011-02-20 13:59:40Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui.journal;

import apiary.common.FormHelper;
import javax.microedition.lcdui.*;

import apiary.Apiary;
import apiary.db.FamilyStore.FamilyRecord;
import apiary.db.HousingStore.HousingRecord;
import apiary.db.PointStore.PointRecord;
import apiary.db.managers.DBManager;
import apiary.ui.*;
import java.util.Vector;

public final class AddInspectForm extends BackScreen
                               implements ItemEventListener,
                                          ItemStateListener,
                                          CommandListener
{
    public static final String STATE_SET = "set";
    public static final String STATE_UNSET = "unset";

    private Command chooseCommand;
    private CategoryItem categoryName;
    private CategoryItem categoryExist;
    private CategoryItem curSel;
    
    private Vector inspectH;
    private CategoryItem existHousing;
    private TextField nameHousing;

    AddInspectForm(String name, Vector inspectHValue) {
        super(name);
        inspectH = inspectHValue;

        Apiary apiary = Apiary.getInstance();
        chooseCommand = new Command(apiary.getLocaleString("Choose"), Command.ITEM, 0);
        addCommand(new Command(apiary.getLocaleString("Add"), Command.SCREEN, 1));
        addCommand(new Command(apiary.getLocaleString("Cancel"), Command.SCREEN, 2));
        
        categoryName= FormHelper.createCategoryItem(this, "",
                apiary.getLocaleString("EnterName"), "/res/ArrowDown.png");
        append(categoryName);
        categoryExist = FormHelper.createCategoryItem(this, "",
                apiary.getLocaleString("ChooseExist"), "/res/ArrowRight.png");
        append(categoryExist);
        nameHousing = new TextField(apiary.getLocaleString("NameHousing"),
                                    "", 256, TextField.ANY);
        itemNotifyEvent(categoryName, ItemEventListener.PRESS);
        
        setCommandListener(this);
        setItemStateListener(this);
    }

    public void itemNotifyEvent(Item item, String event) {
        if(event.equals(ItemEventListener.PRESS)){
            CategoryItem category = (CategoryItem) item;
            if(existHousing != null)
                existHousing.setState(STATE_UNSET);
            if(item == categoryName) {
                if(categoryName.getState().equals(STATE_SET))
                    return;
                existHousing = null;
                categoryExist.setState(STATE_UNSET);
                categoryExist.setImage("/res/ArrowRight.png");
                categoryName.setImage("/res/ArrowDown.png");
                deleteExistHItems();
                insert(1, nameHousing);
            } else if(item == categoryExist) {
                if(categoryExist.getState().equals(STATE_SET))
                    return;
                existHousing = null;
                categoryName.setState(STATE_UNSET);
                categoryName.setImage("/res/ArrowRight.png");
                categoryExist.setImage("/res/ArrowDown.png");
                delete(1);
                addExistHItems();
            } else {
                if(existHousing != null)
                    existHousing.setImage("/res/Checkbox.png");
                category.setImage("/res/CheckboxCheck.png");
                existHousing = category;
            }
            category.setState(STATE_SET);
        }
    }

    public void itemStateChanged(Item item) {
        if(item.getClass() != CategoryItem.class)
            return;
        CategoryItem categoryItem = (CategoryItem) item;
        Apiary apiary = Apiary.getInstance();
        if(categoryItem.getActive()) {
            addCommand(chooseCommand);
            curSel = (CategoryItem) item;
        } else {
            removeCommand(chooseCommand);
            curSel = null;
        }
    }

    public void commandAction(Command c, Displayable d) {
        if(c == chooseCommand)
            itemNotifyEvent(curSel, PRESS);
        else if(c.getPriority() == 1) {
            Apiary.getInstance().Display(backForm);
            addInspectHousing();
        } else if (c.getPriority() == 2)
            Apiary.getInstance().Display(backForm);
    }

    private void deleteExistHItems() {
        while(size() > 2)
            delete(2);
    }

    private void addExistHItems() {
        addHousingsList(0);
        DBManager dBManager = DBManager.getInstance();
        Vector points = dBManager.getPoints();
        for(int i = 0; i < points.size(); i++) {
            PointRecord point = (PointRecord) points.elementAt(i);
            Vector families = dBManager.getFamilies(point.idPoint);
            for(int j = 0; j < families.size(); j++) {
                FamilyRecord family = (FamilyRecord) families.elementAt(j);
                int pos = append(point.namePoint + ": " + family.name);
                if(!addHousingsList(family.idFamily))
                    delete(pos);
            }
        }
    }

    private boolean addHousingsList(int idFamily) {
        DBManager dBManager = DBManager.getInstance();
        Vector housings = dBManager.getHousings(idFamily);
        if(housings.isEmpty())
            return false;
        for(int i = 0; i < housings.size(); i++) {
            HousingRecord record = (HousingRecord) housings.elementAt(i);
            CategoryItem item = FormHelper.createCategoryItem(this,
                    String.valueOf(record.idHousing), record.name, "/res/Checkbox.png");
            item.setState(STATE_UNSET);
            append(item);
        }
        return true;
    }

    private void addInspectHousing() {
        InspectValue inspect = new InspectValue();
        if(existHousing == null)
            inspect.nameHousing = nameHousing.getString();
        else {
            inspect.idHousing = Integer.parseInt(existHousing.getId());
            inspect.nameHousing = existHousing.getName();
        }
        for(int i = 0; i < inspectH.size(); i++) {
            InspectValue value = (InspectValue) inspectH.elementAt(i);
            if(value.equal(inspect))
                return;
        }
        CategoryItem item = FormHelper.createCategoryItem(
                                            (ItemEventListener) backForm,
                                            String.valueOf(inspect.idHousing),
                                            inspect.nameHousing, "");
        backForm.append(item);
        inspectH.addElement(inspect);
    }
}
